home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / REVERSEM.ZIP / MYPIC.CPP < prev    next >
C/C++ Source or Header  |  1994-10-09  |  5KB  |  204 lines

  1. /*--------------------------------------------------------------------------
  2.  
  3.   REVERSE :
  4.  
  5.   UNIT    : My picture
  6.  
  7.  
  8.  
  9. ---------------------------------------------------------------------------*/
  10.  
  11.  
  12. // ------------------------------------------------------------------------
  13. // --- INCLUDES
  14. // ------------------------------------------------------------------------
  15. #include<stdlib.h>
  16. #include<stdio.h>
  17. extern "C" {
  18.    #include "xtile21!.h"
  19. };
  20. #include<string.h>
  21. #include<alloc.h>
  22. #include"reversem.hpp"
  23. #include"mypic.hpp"
  24.  
  25. // ------------------------------------------------------------------------
  26. // --- DEFINITIONS
  27. // ------------------------------------------------------------------------
  28.  
  29. // --- Definitions ---------------------------------------------------------
  30.  
  31. #define  PICTUREX_SIZE          100
  32. #define  PICTUREY_SIZE          100
  33. #define  PICTURE_DATA_SIZE    (PICTUREX_SIZE * PICTUREY_SIZE)
  34.  
  35. // -- Screen location for picture
  36. #define  PICTUREX_LOC        4
  37. #define  PICTUREY_LOC           31
  38.  
  39. // -- Text definitions
  40. #define  TEXT_X            8
  41. #define  TEXT_Y              134
  42. #define  TEXT_X_SIZE           92
  43. #define  TEXT_Y_SIZE           22
  44. #define  TEXT_COLOR          103
  45. #define  TEXT_BACKGROUND      123
  46.  
  47. // ------------------------------------------------------------------------
  48. // --- PRIVATE MEMBERS
  49. // ------------------------------------------------------------------------
  50.  
  51. // -- Members :  -----------------------------------------------------
  52. void  MyPicture::LoadPicture( AVAILABLE_PICTURES Which,
  53.                   unsigned int       FindPic ) {
  54.  
  55.    unsigned char  *PicLocation;
  56.    long           PicOffset;
  57.  
  58.    // Generate a pointer and a file offset.
  59.    PicLocation = PicDump + (FindPic * PICTURE_DATA_SIZE);
  60.    PicOffset   = (long)((long)(Which) * (long)(PICTURE_DATA_SIZE));
  61.  
  62.    fseek( PicFile, PicOffset, SEEK_SET );
  63.    fread( PicLocation, PICTURE_DATA_SIZE, 1, PicFile );
  64.  
  65. };
  66.  
  67.  
  68. void  MyPicture::DisplayPicture( unsigned int  FindPic ) {
  69.  
  70.    XWait_Retrace();
  71.    XPut_Tile( PICTUREX_LOC, PICTUREY_LOC, PICTUREX_SIZE, PICTUREY_SIZE,
  72.           PicDump + (FindPic * PICTURE_DATA_SIZE) );
  73. };
  74.  
  75.  
  76. inline void  MyPicture::ClearText( void ) {
  77.  
  78.    XSet_Box( TEXT_X, TEXT_Y, TEXT_X_SIZE, TEXT_Y_SIZE, TEXT_BACKGROUND );
  79.  
  80. };
  81.  
  82.  
  83. // ------------------------------------------------------------------------
  84. // --- CONSTRUCTOR/DESTRUCTOR
  85. // ------------------------------------------------------------------------
  86.  
  87. MyPicture::MyPicture( void ) {
  88.  
  89.    int  Step;
  90.  
  91.    // Open pic file, and allocate pic dump.  For now, any error is fatal.
  92.    PicFile = fopen( "mypics.cmb", "rb" );
  93.    if ( PicFile == NULL ) {
  94.       puts( ".. FATAL ERROR ..  Unable to open 'mypics.cmb' file.\n\r\n\r");
  95.       exit( 1 );
  96.    }
  97.    PicDump = (unsigned char *) farmalloc( (unsigned long) PICTURE_DATA_SIZE * MAX_LOADED_PICTURES );
  98.    if ( PicDump == NULL ) {
  99.       puts( ".. FATAL ERROR ..  Unable to allocate memory for my pictures.\n\r\n\r");
  100.       exit( 1 );
  101.    }
  102.  
  103.    // Clear the pictures loaded array.
  104.    for (Step = 0; Step < MAX_LOADED_PICTURES; Step++ ) {
  105.       LoadPics[Step] = NO_PICTURE;
  106.    }
  107.  
  108.    UseTick = 0;
  109.  
  110. };
  111.  
  112.  
  113. MyPicture::~MyPicture() {
  114.  
  115.    // Close file and return memory
  116.    fclose(  PicFile );
  117.    farfree( PicDump );
  118.  
  119. };
  120.  
  121.  
  122. // ------------------------------------------------------------------------
  123. // --- PUBLIC MEMBERS
  124. // ------------------------------------------------------------------------
  125. void  MyPicture::Reset( void ) {
  126.  
  127.    int  Step;
  128.  
  129.    // Reset the picture system.  Clear the loaded and clear the text window.
  130.    for (Step = 0; Step < MAX_LOADED_PICTURES; Step++ ) {
  131.       LoadPics[Step] = NO_PICTURE;
  132.    }
  133.    UseTick = 0;
  134.  
  135.    ClearText();
  136.  
  137. };
  138.  
  139.  
  140. void  MyPicture::ShowPicture( AVAILABLE_PICTURES  Which ) {
  141.  
  142.    unsigned int  FindPic;
  143.    unsigned int  Step;
  144.  
  145.    UseTick++;
  146.  
  147.    // If the picture requested is loaded, then show it.
  148.    // If it is not loaded, then first load it.
  149.    // If there is no room to load it, then replace the least recently used.
  150.  
  151.    for ( FindPic = 0; FindPic < MAX_LOADED_PICTURES; FindPic++ ) {
  152.  
  153.       if ( LoadPics[FindPic] == Which ) {
  154.      // It is loaded!!
  155.      DisplayPicture( FindPic );
  156.      TickOfLastUse[FindPic] = UseTick;
  157.      return;
  158.       }
  159.    }
  160.  
  161.    // Ok, not loaded.  Find a space to load
  162.    for ( FindPic = 0; FindPic < MAX_LOADED_PICTURES; FindPic++ ) {
  163.  
  164.       if ( LoadPics[FindPic] == NO_PICTURE ) {
  165.  
  166.      // It is an empty spot!!
  167.      LoadPicture( Which, FindPic );
  168.      LoadPics[FindPic] = Which;
  169.      DisplayPicture( FindPic );
  170.      TickOfLastUse[FindPic] = UseTick;
  171.      return;
  172.       }
  173.    }
  174.  
  175.    // Ok, boot the least recently used
  176.    FindPic = 0;
  177.    for ( Step = 1; Step < MAX_LOADED_PICTURES; Step++ ) {
  178.       if ( TickOfLastUse[Step] < TickOfLastUse[FindPic] )
  179.      FindPic = Step;
  180.    }
  181.    LoadPicture( Which, FindPic );
  182.    LoadPics[FindPic] = Which;
  183.    DisplayPicture( FindPic );
  184.    TickOfLastUse[FindPic] = UseTick;
  185.  
  186. };
  187.  
  188.  
  189. void   MyPicture::SayText( char*   Line1,
  190.                char*   Line2,
  191.                char*   Line3    ) {
  192.  
  193.    // Display the lines that are not NULL
  194.    ClearText();
  195.  
  196.    if( Line1 != NULL )
  197.       XString4_C( TEXT_X, TEXT_Y,    TEXT_COLOR, Line1 );
  198.    if( Line2 != NULL )
  199.       XString4_C( TEXT_X, TEXT_Y+6,  TEXT_COLOR, Line2 );
  200.    if( Line3 != NULL )
  201.       XString4_C( TEXT_X, TEXT_Y+12, TEXT_COLOR, Line3 );
  202.  
  203. };
  204.